require(['jquery',"bootstrap","bbcare",
    "scprit/followup/app/prematureOvarianFailure/womenHealthQuestionnaire.ui.js",
    "scprit/followup/app/prematureOvarianFailure/prematureOvarianFailureDetail.ui.js",
    "scprit/followup/app/prematureOvarianFailure/surveyOfWomenHealth.servers.js",
    "scprit/followup/app/prematureOvarianFailure/womenHealthQuestionnaire.event.js"
    ],function($,bootstrap,$bb,$ui,$detailUi,$servers,$event){
    var getInfo = $bb.getPageParams();
    $detailUi.basicInfoInit({"id":"#userInfo","data":getInfo});//基本信息渲染：
    /*
    * 获取问卷：
    * */
    var params= $.extend({},getInfo);
    var getQuestionCallback = function(data,params){
        if(data.success){
            var questions = [];
            for(var item in data.data){
                if(item != "一般人口学信息"){
                    questions.push({"title":item,"group":item,"questions":data.data[item]});
                }
            }
            getTotalQuestions_osiList(questions);
            var scoreObj = {"ques":{},"mark":{}};
            $ui.layoutBoxInit({"id":'#box'});
            $ui.layoutInit({"id":"#wrap"});
            $ui.projectScheduleInit({"id":"#wrap_right","data":questions});
            var totalQuestions = getTotalQuestions_osi(questions);
            var questions3 = getQuestions_osi(totalQuestions);
            //默认展示第一题：
            $ui.questionTypeInit2({"id":"#questionnaire","data":questions3[1]});
            $event.init2(questions3,scoreObj,params,totalQuestions.length);
        }
    };
    $servers.getQuestions(getInfo,getQuestionCallback,params);
    /*
    * osi问卷
     */
    var topicType = function(des1,list){
        var des3 = [];
        for(var j = 0;j<des1.length;j++){
            var text2;
            if(des1[j].indexOf("text") == 0){
                text2 = "text";
                if(des1[j].indexOf("-") >0){
                    var leiXing2 = des1[j].substring(des1[j].indexOf("-")+1);
                    if(leiXing2 == "boxes"){
                        list.type = "blankList";//需要填入多条；
                    }else if(leiXing2 == "num"){
                        text2 = "number";
                    }else if(leiXing2 == "time"){
                        text2 = "time";
                    }else if(leiXing2 == "date"){
                        text2 = "date";
                    }
                }
            }else{
                text2 = des1[j];
            }
            des3.push({"item":text2});
        }
        return des3;
    }
    var getTotalQuestions_osi = function(questions){
        var totalQuestions = [];
        for(var i = 0; i< questions.length;i++){
            for(var k = 0;k<questions[i].questions.length;k++){
                questions[i].questions[k].itemTitle = questions[i].title;//获取每个group的问卷长度：
                if(questions[i].questions[k].list){//联动类题型获取id；
                    var childId = questions[i].questions[k].list[0].id;
                    if(childId && childId.indexOf("-") >= 0){
                        questions[i].questions[k].id=childId.substring(0,childId.indexOf("-"));
                    }else{
                        questions[i].questions[k].id=childId;
                    }
                    var list = questions[i].questions[k].list;
                    var link=[],linkArr = [];//关联的
                    for(var m = 0;m<list.length;m++){
                        var linkId = list[m].id;
                        if($.inArray(linkId,link) >= 0){
                            list[m].css = "none";
                        }
                        if(list[m].option){//选择：
                            if(list[m].choice && list[m].choice != "redio"){
                                list[m].type = "checkbox";
                            }else{
                                //题目带输入框：
                                if(typeof list[m].description == "object"){
                                    list[m].description.unshift(list[m].id);
                                    if(list[m].id == "2.13.5"){
                                        list[m].description.splice(4,2);
                                    }
                                    list[m].description = topicType(list[m].description,list[m]);
                                    list[m].type = "blankSingle";
                                }else{
                                    list[m].description = list[m].id + list[m].description;
                                    list[m].type = "single";
                                }
                            }
                            if(list[m].choose && typeof list[m].choose == 'object'){
                                linkArr = list[m].link0.concat(list[m].link1);
                                link = link.concat(linkArr);
                                list[m].choose = JSON.stringify(list[m].choose);
                                list[m].link0 = JSON.stringify(list[m].link0);
                                list[m].link1 = JSON.stringify(list[m].link1);
                            }
                            if(list[m].link){
                                link = link.concat(list[m].link);
                                list[m].link = JSON.stringify(list[m].link);
                            }
                            list[m].option = optionDeal(list[m].option);
                        }else{
                            list[m].type = "blank";//填空：
                            var des1 = list[m].description;
                            if(typeof des1 == 'string'){
                                des1 = [des1];
                            }
                            if(list[m].id && list[m].id.indexOf(".")>= 0){
                                des1.unshift(list[m].id);
                            }
                            list[m].description = topicType(des1,list[m]);
                        }
                        list[m].itemTitle = questions[i].title;
                        if(list[m].id.indexOf(".")>= 0){
                            var splitId = list[m].id.split(".");
                            list[m].id = splitId.join("-");
                        }
                    }
                    questions[i].questions[k].type = "linkage";//联动类型：
                }else{
                    if(questions[i].questions[k].option){//选择：
                        if(questions[i].questions[k].choice && questions[i].questions[k].choice != "redio"){
                            questions[i].questions[k].type = "checkbox";
                        }else{
                            questions[i].questions[k].type = "single";
                        }
                        if(questions[i].questions[k].id.indexOf(".")>= 0){
                            questions[i].questions[k].description = questions[i].questions[k].id+questions[i].questions[k].description;
                        }
                        questions[i].questions[k].option = optionDeal(questions[i].questions[k].option);
                    }else{
                        questions[i].questions[k].type = "blank";//需要填入多条；
                        var des = questions[i].questions[k].description;
                        if(questions[i].questions[k].id.indexOf(".")>= 0){
                            des.unshift(questions[i].questions[k].id);
                        }
                        questions[i].questions[k].description = topicType(des,questions[i].questions[k]);
                    }
                }
                if(questions[i].questions[k].id.indexOf(".")>= 0){
                    var splitId = questions[i].questions[k].id.split(".");
                    questions[i].questions[k].id = splitId.join("-");
                }
                questions[i].questions[k].extra = [{"num":1},{"num":2}];//备注：
                totalQuestions.push(questions[i].questions[k]);
            }
        }
        return totalQuestions;
    };
    var getQuestions_osi = function(totalQuestions){
        var questions = {};
        for(var i = 0; i< totalQuestions.length;i++){
            totalQuestions[i].listNo = i+1;
            questions[i+1] = totalQuestions[i];
        }
        return questions
    };
    var optionDeal = function(options){
        var option = [];
        for(var item in options){
            option.push({"item":item,"val":options[item]});
        }
        return option;
    }
    var listDeal = function(list,ques){
        var ss= [];
        for(var m = 0;m<list.length;m++) {
            if(list[m].list) {
                var listS = list[m].list;
                for(var i = 0;i < listS.length;i++){
                    if(listS[i].list){
                        for(var j = 0;j < listS[i].list.length;j++){
                            listS[i].list[j].css ="none";
                            ss.push(listS[i].list[j]);
                        }
                    }else{
                        ss.push(listS[i]);
                    }
                }
            }else{
                ss.push(list[m]);
            }
        }
        ques.list = ss;
    };

    var getTotalQuestions_osiList = function(questions){
        for(var i = 0; i< questions.length;i++){
            for(var k = 0;k<questions[i].questions.length;k++){
                if(questions[i].questions[k].list){
                    listDeal(questions[i].questions[k].list, questions[i].questions[k]);
                }
            }
        }
    };
});